package com.qf.usermanage;

import java.util.Random;
import java.util.Scanner;

/**
 * 该类负责显示菜单，并且根据用户操作执行不同的功能
 */
public class Menu {
    //执行该方法会显示菜单
    UserService userService;

    public Menu() {
        this.userService = new UserService();
    }

    public static void main(String[] args) {
        Menu menu = new Menu();
        menu.printWelcome();
        menu.show();
    }
    public String code() { //随机生成验证码
        String[] s = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"};
        Random r = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(s.length);
            code = code + s[index];
        }
        return code;
    }
    public void printWelcome(){
        System.out.println("welcome to 用户管理系统");
    }
    public  void show(){
        Scanner sc = new Scanner(System.in);
//        UserService userService = new UserService();
        while(true){
            System.out.println("1.用户登录\t2.用户注册\t3.退出");
            int menuIndex = sc.nextInt();
            switch (menuIndex){
                case 1:
                    System.out.println("请输入用户名");
                    String username = sc.next();
                    System.out.println("请输入密码");
                    String password = sc.next();

                    String code = code();
                    System.out.println("请输入验证码"+code);
                    String cap =sc.next();
                    User user = new User(username,password);
                    boolean login = userService.login(user);
                    if(code.equals(cap)){
                        if (userService.login(user)){
                            System.out.println("登录状态"+login);
                        }else{
                            System.out.println("登录状态"+login);
                        }
                    }else{
                        System.out.println("登录状态"+login);
                    }


                    break;
                case 2:
                    System.out.println("请输入用户名");
                    String name = sc.next();
                    System.out.println("请输入密码");
                    String pwd = sc.next();
                    System.out.println(2);
                    boolean register = userService.register(new User(name,pwd));
                    System.out.println("注册状态"+register);
                    break;
                case 3:
                    System.out.println("退出,欢迎下次使用");
                    return;
                default:
                    System.out.println("输入错误");
            }
        }
    }
}
